/**
 * Created by user on 2017/9/22.
 */
import { LOGIN } from './ActionTypes';
import { Request } from '../utils'
import { message } from 'antd';
import { push } from 'react-router-redux';
import Storage from '../storage';

const handleLoading = ( loading )=>{
    return {
        type:LOGIN.LOADING,
        payload:loading
    }
};
const do_login = (data,to_path)=>{
    return (dispatch)=>{
        dispatch(handleLoading(true));
        Request.SendRequest('login','POST',data,(res)=>{
            if(res.state == 200) {
                dispatch(login_success(res.data));
                // dispatch(push(to_path ? to_path : '/about'));
            }else{
                message.error('登录失败,'+res.message);
            }

        },(err)=>{
            message.error('登录失败,请稍后重试');
        },()=>{
            dispatch(handleLoading(false));
        })
    }
};
const do_logout = ()=>{
    return (dispatch)=>{
        console.log(111)
        dispatch(logout_success());
        dispatch(push('/login'));
    }
};
const login_success = (user)=>{
    Storage.write('login_user',user, 24*60*60);
    return {
        type:LOGIN.LOGIN_SUCCESS,
        payload:user
    }
};
const logout_success = ()=>{
    Storage.remove('login_user');
    return {
        type:LOGIN.LOGOUT_SUCCESS
    }
};
module.exports = {
    handleLoading:handleLoading,
    do_login:do_login,
    do_logout:do_logout
};